const express = require('express');
const router = express.Router();
const weatherController = require('../controllers/weatherController');
const logger = require('../utils/logger');

/**
 * @route GET /api/weather/current
 * @desc 获取当前天气
 * @access Public
 * @query {string} city - 城市名称
 */
router.get('/current', weatherController.getCurrentWeather);

/**
 * @route GET /api/weather/forecast
 * @desc 获取天气预报
 * @access Public
 * @query {string} city - 城市名称
 * @query {number} days - 预报天数（1-7）
 */
router.get('/forecast', weatherController.getWeatherForecast);

/**
 * @route GET /api/weather/default
 * @desc 获取默认城市的天气
 * @access Public
 */
router.get('/default', weatherController.getDefaultWeather);

/**
 * @route PUT /api/weather/default
 * @desc 设置默认城市
 * @access Public
 * @body {string} city - 城市名称
 */
router.put('/default', weatherController.setDefaultCity);

// 错误处理中间件
router.use((err, req, res, next) => {
  logger.error(`天气路由错误: ${err.message}`);
  
  // 处理API错误
  if (err.code) {
    return res.status(400).json({
      success: false,
      message: err.message,
      code: err.code
    });
  }
  
  // 其他错误
  res.status(500).json({
    success: false,
    message: err.message || '服务器内部错误'
  });
});

module.exports = router;